import { PageConfig } from 'next';
import an48 from '@mmstudio/an000048';
import anylogger from 'anylogger';
import '@mmstudio/an000042';
import an51 from '@mmstudio/an000051';
import a005 from '../../../atoms/getuser';
import a003 from '../../../atoms/key';

const logger = anylogger('pages/api/account/logout.api');

export type Result = {
	ok: true;
} | {
	ok: false;
	message: string;
};

/**
 * 退出登陆
 */
const handler = an48<Result>();

handler.get(async (req, res) => {
	try {
		logger.debug('msg body:', req.body);
		const user = await a005(req);
		if (user) {
			const key = a003();
			an51(req, res, key, {
				sameSite: 'lax'
			});
		}
		res.redirect('/');
	} catch (error) {
		logger.error(error);
		res.redirect('/');
	}
});

export const config = {} as PageConfig;

export default handler;
